<template>
	<el-form ref="form" size="medium" label-position="top" class="system_config_container">
		<el-form-item v-for="(item, index) in constDatas" :key="index" :label="item.content">
			<template v-if="item.type === '1'">
				<el-input-number v-model="item.name" controls-position="right" :min="1" />
				<el-button type="primary" @click="handleUpdateConfig(item)">确定</el-button>
			</template>
			<template v-if="item.type === '2'">
				<el-switch v-model="item.name" active-value="1" inactive-value="0" @change="handleUpdateConfig(item)" />
			</template>
			<template v-if="item.type === '3'">
				<el-input v-model="item.name"></el-input>
			</template>
			<template v-if="item.type === '4'">
				<el-input v-model="item.name"></el-input>
			</template>
			<template v-if="item.type === '5'">
				<el-input v-model="item.name"></el-input>
			</template>
			<template v-if="item.type === '6'">
				<el-input v-model="item.name"></el-input>
				<el-button type="primary" @click="handleUpdateConfig(item)">确定</el-button>
			</template>
			<template v-if="item.type === '7'">
				<el-input v-model="item.name"></el-input>
			</template>
			<template v-if="item.type === '8'">
				<el-input v-model="item.name"></el-input>
			</template>
			<p class="tips" v-if="!!item.tips">{{ item.tips }}</p>
		</el-form-item>
	</el-form>
</template>

<script>
import { getDictDefineList, updateDictDefine } from '@/api/system/tbSysDictDefinition';
export default {
	data() {
		return {
			formData: {},
			constDatas: [],
			dictConfigs: [
				{ type: '6', code: 'USER_PASSWORD', tips: '配置用户初始密码' },
				{ type: '1', code: 'PASS_DURATION', tips: '配置用户学习达标学时' },
				{ type: '2', code: 'VAILCODE', tips: '开启之后，登录时需要输入验证码' },
				{ type: '2', code: 'GRAY_SITE', tips: '开启之后，网站系统页面置灰' },
				{ type: '1', code: 'SYSTEM_ATTA_SIZE', tips: '配置系统附件大小' },
				{ type: '1', code: 'BUSI_ATTA_SIZE', tips: '配置业务附件大小' }
			]
		};
	},
	watch: {},
	mounted() {
		this.getDictConstsData();
	},
	methods: {
		//获取字典常量数据
		async getDictConstsData() {
			let params = { page: 1, limit: 9999, type: '2' };
			let result = await getDictDefineList(params).then((res) => res.data);
			if (result.code === '0') {
				let datas = result.data || [];
				this.constDatas = this.dictConfigs.map((item) => {
					let data = datas.find((n) => n.code === item.code);
					return { ...data, tips: item.tips, type: item.type };
				});
			}
		},

		//更新配置
		async handleUpdateConfig(data) {
			let params = { id: data.id, name: data.name };
			let result = await updateDictDefine(params).then((res) => res.data);
			if (result.code === '0') {
				this.$message.success('更新成功');
			}
		}
	}
};
</script>
<style lang="scss" scoped>
.system_config_container::v-deep {
	height: 100%;
	padding: 24px;
	overflow: auto;
	box-sizing: border-box;
	.el-form-item {
		margin-bottom: 32px;
	}
	.el-form-item__label {
		font-weight: bold;
		padding-bottom: 12px;
	}
	.el-form-item__content {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}
	.el-input {
		width: auto;
	}
	.el-button {
		margin-left: 10px;
	}
	.tips {
		width: 100%;
		color: #909399;
		line-height: 1;
		margin-top: 12px;
	}
}
</style>
